package com.yhy.tradesystem.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Log {
	
	public static String TAG_FILTER = null;
	
	public static FileOutputStream fos = null;
	
	public static boolean consoleOuput = true; 
	
	public static boolean startRecordLogFile(String logfileName){
		try {
			fos = new FileOutputStream(logfileName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			fos =null;
			return false;
		}
		return true;
	}
	public static void stopRecordLogFile(){
		if(fos!=null){
			try {
				fos.close();
				fos = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void d(String TAG, String format, Object... args) {
		if(TAG_FILTER!=null && !TAG_FILTER.equals(TAG)){
			return;
		}
		String log = "TAG:"+TAG+" "+String.format(format, args)+"\n";
		if(consoleOuput){
			System.out.printf(log);
		}
		
		if(fos!=null){
			try {
				fos.write(log.getBytes());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public static void e(String TAG, String format, Object... args) {
		if(TAG_FILTER!=null && !TAG_FILTER.equals(TAG)){
			return;
		}
		if (consoleOuput) {
			System.out.printf("***********ERROR*******************************"
					+ "\n");
			System.out.printf("TAG:" + TAG + " " + String.format(format, args)
					+ "\n");
			System.out.printf("***********************************************"
					+ "\n");
		}
	}
}
